package com.tomcov.client.activities;

import com.google.gwt.activity.shared.AbstractActivity;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.maps.client.geocoder.HasGeocoder;
import com.google.gwt.place.shared.Place;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.AcceptsOneWidget;
import com.tomcov.client.ClientFactory;
import com.tomcov.client.place.MapPlace;
import com.tomcov.client.place.MessageInPlace;
import com.tomcov.client.place.MessageOutPlace;
import com.tomcov.client.place.SettingsPlace;
import com.tomcov.client.place.WelcomPlace;
import com.tomcov.client.service.JourneyServiceAsync;
import com.tomcov.client.view.MapView;
import com.tomcov.shared.Journey;

public class MapActivity extends AbstractActivity implements MapView.Presenter{

	private ClientFactory clientFactory;
	private HasGeocoder geocoder;

	
	public MapActivity(ClientFactory clientFactory){
        this.clientFactory = clientFactory;	
	}
	
	@Override
	public void start(AcceptsOneWidget panel, EventBus eventBus) {
		final MapView mapView = clientFactory.getMapView();
		mapView.getRoadButton().addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				mapView.getDirectionsPanel().clear();
				getRoad();	
			}
		});
		mapView.getAddJourneyButton().addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				addjourney();	
			}
		});
		mapView.getLogoutLabel().addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				goTo(new WelcomPlace(null));		
			}
		});
		mapView.getMapLabel().addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				goTo(new MapPlace(null));		
			}
		});
		mapView.getMyjourneyLabel().addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				//goTo(new MapPlace(null));		
			}
		});
		mapView.getSettingsLabel().addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				goTo(new SettingsPlace(null));		
			}
		});
		mapView.getMessageInLabel().addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				goTo(new MessageInPlace(null));		
			}
		});
		mapView.getMessageOutLabel().addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				goTo(new MessageOutPlace(null));		
			}
		});
		
		mapView.setPresenter(this);
        panel.setWidget(mapView.asWidget());

	}

	@Override
	public void goTo(Place place) {
		clientFactory.getPlaceController().goTo(place);		
	}


	private void getRoad() {
		// TODO Auto-generated method stub
		
	}

	private void addjourney() {
		MapView mapview=clientFactory.getMapView();
		JourneyServiceAsync JSA =clientFactory.getJSA();
		Journey journey=new Journey(mapview.getFromAddress().getText(),mapview.getToAddress().getText(),null,null);
		
		JSA.AddJourney(journey,new AsyncCallback<Journey>(){
			@Override
			public void onFailure(Throwable caught) {
				Window.alert("fail to add journey");		
			}
			@Override
			public void onSuccess(Journey result) {
				if(result==null){
					Window.alert("non non");
				}
				else{
					Window.alert("journey add");
				}
			}
		});
		
	}

}